<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/26
 * Time: 14:14
 */

/**
 * Class Involved_UserModel
 */

class Involved_UserModel extends BaseModel{

    protected $table = 'user';


    /**
     * 获取用户登录信息
     * @param $mobile
     * @param $pwd
     * @return mixed
     * @author bao
     * @date 2016-01-27
     */
    public function login($mobile,$pwd)
    {
       $info =  DB::table($this->table)
                ->select('user.user_id','user.user_name','user.mobile','user.avatar','user.address','user.id_card','user.phone','user.is_supplier')
                ->where(['mobile'=>$mobile,'password'=>$pwd])
                ->first();
        if(!empty($info) && empty($info->avatar))
            $info->avatar = '/resource/img/user_img.png';

        return $info;
    }

    /**
     * 获取用户信息
     * @param $user_id
     * @return mixed
     * @author alice
     * @date 2016-1-26
     */
    public function getUserInfo($user_id)
    {
        $info =  DB::table($this->table)
                ->select('user_id','user_name','is_supplier','mobile','avatar','phone','corporation','address')
                ->where('user_id',$user_id)
                ->first();
        if(!empty($info) && empty($info->avatar))
            $info->avatar = '/resource/img/user_img.png';

        return $info;
    }




    /**
     * 获取密码
     * @param $params
     * @return mixed
     */
    public function getPassword($params)
    {
        return DB::table($this->table)->where('user_id',$params['user_id'])->select('password')->first();
    }

    //验证密码
    public function UserPasswordSelect($user_id,$old_password){
        return DB::table($this->table)
            ->where('user_id' , $user_id)
            ->where('password' , $old_password)
            ->count();

    }
    public function GetUserId($mobile)
    {
        return DB::table($this->table)
                ->select('user_id')
                ->where('mobile', $mobile)
                ->first();
    }

    /**
     * 获取用户名
     * @param $mobile
     * @return mixed
     */
    public function GetMobile($mobile)
    {
        return DB::table($this->table)->where('mobile',$mobile)->count();
    }

    /**
     * 验证密码
     * @param $user_id
     * @param $password
     * @return mixed
     */
    public function CheckPassword($user_id, $password)
    {
        return DB::table($this->table)->where(['user_id' => $user_id, 'password' => $password])->count();
    }

    /**
     * 修改密码
     * @param $new_pass
     * @param $user_id
     * @return mixed
     */
    public function ModifyPassword($new_pass, $user_id)
    {
        return DB::table($this->table)->where(['user_id' => $user_id])->update(['password' => md5($new_pass)]);
    }

    /**
     * 重置密码
     * @param $new_pass
     * @param $mobile
     * @return mixed
     */
    public function ResetPassword($new_pass, $mobile)
    {
        return DB::table($this->table)->where(['mobile' => $mobile])->update(['password' => md5($new_pass)]);
    }

     
    /**
     * 根据用户id更新用户信息
     * @param int $user_id
     * @param array $data
     * @param mixed
     */
    public function updateByUserId($user_id, $data) 
    {
        return DB::table($this->table)->where(['user_id' => $user_id])->update($data);
   	}

    public function GetSupplierAvatar($supplier_id)
    {
        return DB::table($this->table)
                ->select('avatar')
                ->where(['user_id' => $supplier_id])
                ->first();
    }



    /**
     * 用户下拉菜单
     * @param mixed
     */
    public function UserSelect()
    {
        return DB::table($this->table)
            ->select('user_id','user_name')
            ->where('is_supplier',0)
            ->get();
    }

    /**
     * js注册验证手机号
     * @param $mobile
     * @return mixed
     */
    public function JsCheckMobile($mobile)
    {
        return DB::table($this->table)
            ->select('mobile')
            ->where('mobile',$mobile)
            ->first();
    }

    /**
     * js登陆验证用户名
     * @param $name
     * @return mixed
     */
    public function JsCheckUser($name)
    {
        return DB::table($this->table)
            ->select('user_name','user_id','mobile')
            ->where('mobile',$name)
            ->first();
    }

    /**
     * js登陆验证密码
     * @param $name
     * @return mixed
     */
    public function JsCheckPwd($name,$pwd)
    {
        return DB::table($this->table)
            ->select('password')
            ->where(['mobile'=>$name,'password'=>$pwd])
            ->first();
    }

    /**
     * 电话列表
     * @param $mobile
     * @return mixed
     */
    public function SelectUserMobile($mobile)
    {
        return DB::table($this->table)
            ->select('user_id','user_name','mobile')
            ->where('mobile', 'like', "{$mobile}%")
            ->take(6)
            ->get();
    }

    /**
     * 验证密码
     * @param $user_id
     * @return mixed
     * @author lzhp
     * @date  2016-09-08
     */
    public function ApiGetUserInfoPwd($user_id)
    {
        return DB::table($this->table)
            ->select('password')
            ->where('user.user_id',$user_id)
            ->first();
    }
    /**
     * 个人主页信息获取
     * UserPerson
     * 09.09
     */
    public function UserPerson($user_id){
        return DB::table($this->table)
            ->select('user_id','mobile','avatar','is_supplier')
            ->where('user.user_id',$user_id)
            ->first();

    }
    //头像修改
    public function AvatarUpdate($user_id,$avatar){
        return  DB::table($this->table)->where('user_id',$user_id)->update(array('avatar' => $avatar));
    }

    /**
     * 获取所有用户ID
     * @param mixed
     */
    public function GetAllUserId()
    {
        return DB::table($this->table)
            ->select('user_id')
            ->get();
    }

}